"use strict";

module.exports.getdatestr = function(ft,m,d,dt){
    let dtstr = '';
    if(!dt) 
        dt = new Date();
    else if(typeof dt === 'string')
        dt = new Date(dt);
    if(m) dt.setMonth(dt.getMonth()+m);
    if(d) dt.setDate(dt.getDate()+d);
    for(let i=0;i<ft.length;i++)
    {
        switch(ft[i]){
            case 'Y':{
                dtstr += dt.getFullYear();
                break;
            }case 'y':{
                dtstr += dt.getFullYear().toString().substr(2,2);
                break;
            }case 'm':{
                dtstr += (dt.getMonth() < 9?'0':'') + (dt.getMonth() + 1);
                break;
            }case 'd':{
                dtstr += (dt.getDate() < 10?'0':'') + dt.getDate();
                break;
            }case 'H':
            case 'h':{
                dtstr += (dt.getHours() < 10?'0':'') + dt.getHours();
                break;
            }case 'i':
            case 'I':{
                dtstr += (dt.getMinutes() < 10?'0':'') + dt.getMinutes();
                break;
            }case 's':
            case 'S':{
                dtstr += (dt.getSeconds() < 10?'0':'') + dt.getSeconds();
                break;
            }default:{
                dtstr += ft[i];
                break;
            }
        }
    }
    return dtstr;
}
//获取cookie对象
module.exports.getCookie = (req)=>{
    var ck = {};
    req.headers.cookie && req.headers.cookie.split(';').forEach(function(Cookie) {
        var parts = Cookie.split('=');
        ck[parts[0].trim()] = (parts[1] || '').trim();
    });
    return ck;
}

//执行多条sql语句
module.exports.DBdata = (sqlarr, rfun)=>{
    const DB = require("../mssql");
    let rs = [], i = 0;
    let ex = ()=>{
        if(typeof sqlarr[i] === "object") //分页语句
        {
            if(sqlarr[i].sql && sqlarr[i].orderby)
            {
                sqlarr[i] = "select top " + (sqlarr[i].ps || 100) + " * from (select ROW_NUMBER() OVER(" + sqlarr[i].orderby + ") rownum,* from (" + sqlarr[i].sql + ") Y) Z where rownum > " + ((sqlarr[i].pg || 1) - 1)*(sqlarr[i].ps || 100);
            }
            else
                console.log('not defined order by string');
        }
        DB.executeSql(sqlarr[i], (rows)=>{
            rs[i] = rows;
            ++i;
            if(i >= sqlarr.length) 
                rfun(rs);
            else
                ex();
        });
    }
    ex();
}

//获取post数据
module.exports.getPost = (req, rfun)=>{
    let d = Buffer.allocUnsafe(0);
    req.on("data", (chunk)=>{
        d = Buffer.concat([d, chunk], d.length + chunk.length);
    });
    req.on('end', ()=>{
        rfun(d);
    });
}

//获取post对象
module.exports.getPostObj = (req, rfun)=>{
    const querystring = require("querystring");
    let d = '';//console.log("getPostObj");
    req.on("data", (chunk)=>{
        d += chunk;//console.log("getPostObj data");
    });
    req.on('end', ()=>{ //console.log("getPostObj end");
        rfun(querystring.parse(d));
    });
}

//数组转对象
module.exports.arrToObject = (arr)=>{
    let ls = [];
    for(let k in arr)
    {
        for(let i=0;i<arr[k].length;i++)
        {
            if(!ls[i]) ls[i] = {};
            ls[i][k] = arr[k][i];
        }
    }
    return ls;
}

/** 获取redis数据库 */
module.exports.RS = require("./redis");
/** pg数据库 */
module.exports.DB = require("./pgsql");